package com.unsiv.blplayer.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public final class MyDBHelper extends SQLiteOpenHelper {
	public static MyDBHelper myDBHelper;
	public static final String TAG = "ring/DatabaseHelper";
	public static final String DATABASE_NAME = "btplayer.db";
	public static final int DATABASE_VERSION = 2;
	public static final String TABLE_HOTSPOT = "hotspot";
	public static final String TABLE_RECORDBUFFER = "recordbuffer";

	private MyDBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public static MyDBHelper getMyDBHelper(Context context) {
		if (null != myDBHelper)
			myDBHelper = new MyDBHelper(context, DATABASE_NAME, null,
					DATABASE_VERSION);
		return myDBHelper;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_HOTSPOT + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT," + "oidCode INTEGER,"
				+ "name TEXT);");
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_RECORDBUFFER + " ("
				+ "id INTEGER PRIMARY KEY AUTOINCREMENT," + "oidCode INTEGER,"
				+ "name TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// do nothing here
	}

	/**
	 * 判断hotdata是否已经存在
	 * 
	 * @param name
	 * @return
	 */
	public boolean hasHotData(String name) {
		boolean flag = false;
		SQLiteDatabase db = getReadableDatabase();
		String selection = "name=?";
		String[] selectionArgs = new String[1];
		selectionArgs[0] = name;
		Cursor cursor = getReadableDatabase().query(TABLE_HOTSPOT,
				new String[] { "id", "oidCode", "name" }, selection,
				selectionArgs, null, null, null);
		if (cursor.getCount() > 0)
			flag = true;
		cursor.close();
		db.close();
		return flag;
	}
	/**
	 * 获取所有热点
	 * @param name
	 * @return
	 */
	public Cursor getHotDataByOid(int oid) {
		SQLiteDatabase db = getReadableDatabase();
		String selection = "oidCode=?";
		String[] selectionArgs = new String[1];
		selectionArgs[0] = String.valueOf(oid);
		Cursor cursor = getReadableDatabase().query(TABLE_HOTSPOT,
				new String[] { "id", "oidCode", "name" }, selection,
				selectionArgs, null, null, null);
		db.close();
		return cursor;
	}

	/**
	 * 删除热点
	 * @param name
	 */
	public void delHotData(String name) {
		SQLiteDatabase db = getWritableDatabase();
		db.beginTransaction();
		String whereClause = "name=?";
		String[] whereArgs = new String[1];
		whereArgs[0] = name;
		db.delete(TABLE_HOTSPOT, whereClause, whereArgs);
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}
	/**
	 * 添加热点
	 * @param name
	 */
	public void inesrtHotData(long oidCode,String name) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("oidCode",oidCode);
		values.put("name",name);
		db.insert(TABLE_HOTSPOT, null, values);
		db.close();
	}
}
